iOS中searchBar(搜索框)光标初始位置后移
程序员文章站
2023-12-17 14:07:58
废话不多说了,直接给大家贴关键代码了,具体代码如下所示:
#import
@interface searchbar...
废话不多说了,直接给大家贴关键代码了,具体代码如下所示:
#import <uikit/uikit.h> @interface searchbar : uitextfield @property (nonatomic,strong) uibutton *button; + (instancetype)searchbar; @end #import "searchbar.h" @implementation searchbar - (id)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self) { self.font = [uifont systemfontofsize:15]; self.placeholder = @" 输入品种关键字"; //设置边框和边框颜色 self.borderstyle=uitextborderstylenone; self.layer.cornerradius=15.0f; self.layer.maskstobounds=yes; self.layer.bordercolor=[[uicolor colorwithred:224/255.0 green:243/255.0 blue:223/255.0 alpha:1.0]cgcolor]; self.layer.borderwidth= 2.0f; uibutton *button = [[uibutton alloc] initwithframe:cgrectmake(0, 0, 40, 30)]; [button setbackgroundimage:[uiimage imagenamed:@"seachbar_rightview"] forstate:uicontrolstatenormal]; [button settitle:@"搜索" forstate:uicontrolstatenormal]; [button settitlecolor:[uicolor colorwithred:119/255.0 green:166/255.0 blue:16/255.0 alpha:1.0] forstate:uicontrolstatenormal]; button.titlelabel.font = [uifont systemfontofsize:18.0]; button.titlelabel.textcolor = [uicolor colorwithred:119/255.0 green:166/255.0 blue:16/255.0 alpha:1.0]; button.titlelabel.textcolor = [uicolor redcolor]; self.rightview = button; self.rightviewmode = uitextfieldviewmodealways; //这里设置光标位置,让光标位置后移10 self.leftview = [[uiview alloc]initwithframe:cgrectmake(0, 0, 10, 0)]; self.leftviewmode = uitextfieldviewmodealways; } return self; } + (instancetype)searchbar { return [[self alloc] init]; }
下面介绍下ios中设置输入框的光标位置
//这里设置光标位置,让光标位置后移10 textfield.leftview = [[uiview alloc]initwithframe:cgrectmake(0, 0, 10, 0)]; textfield.leftviewmode = uitextfieldviewmodealways;
以上所述是小编给大家介绍的ios中searchbar(搜索框)光标初始位置后移 ,希望对大家有所帮助