我想找出一個解決方案來創建一個UIButton,帶有UIImage(圖標)文本的中心文本,所以它的網站就在按鈕標題前。UIButton - 帶圖標的文本文本
我,但是我正在努力想辦法做到這一點,因爲你不能檢索文本的位置。有什麼想法嗎?這一定是相當普遍的事情。
我想找出一個解決方案來創建一個UIButton,帶有UIImage(圖標)文本的中心文本,所以它的網站就在按鈕標題前。UIButton - 帶圖標的文本文本
我,但是我正在努力想辦法做到這一點,因爲你不能檢索文本的位置。有什麼想法嗎?這一定是相當普遍的事情。
創建UIView
子類,有一個UIImageView
和UILabel
。在該視圖中將標籤放置在圖像視圖的右側。將此視圖添加到您的按鈕並將其水平和垂直居中放置。
你也可以子類UIButton這是一個更好的方法來做到這一點。
我想,它會幫助你。始終以setImage:
和setBackgroundImage:
UIButton *yourBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[yourBtn setBackgroundImage:bgImage forState:UIControlStateNormal];
[yourBtn setBackgroundImage:bgImage forState:UIControlStateHighlighted];
[yourBtn setTitle:titleString forState:UIControlStateNormal];
使用此代碼小心
UIButton *scoreButton=[UIButton buttonWithType:UIButtonTypeCustom];
scoreButton.frame=CGRectMake(0,0,100, 100);
//scoreButton.contentEdgeInsets=UIEdgeInsetsMake(0, 0, 0, 2);
scoreButton.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
scoreButton.titleLabel.font=[UIFont boldSystemFontOfSize:13];
[scoreButton setTitleColor:[UIColor colorWithRed:0.9411 green:0.5647 blue:.2916 alpha:YES] forState:UIControlStateNormal];
[scoreButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
UIImageView *scoreButtonImageView=[[UIImageView alloc]init];
scoreButtonImageView.frame=CGRectMake(0,35,30 ,30);
scoreButtonImageView.image=[UIImage imageNamed:@"leaderboard_score_button.png"];
[scoreButton addSubview:scoreButtonImageView];
使用UIEdgeInsetsMake設置文本的起點和終點。
這樣你的形象會在最左邊,你可以將圖像
使用後寫的文字下面的方法:
UIImageView *yourPlusSign = [UIImageView alloc]initWithImage:[UIImage imageNamed:@"yourPlusSignImageTitle"];
yourPlusSign.frame = CGRectMake(x, y, width, height);//choose values that fit properly inside the frame of your baseButton
//or grab the width and height of yourBaseButton and change accordingly
yourPlusButton.contentMode=UIViewContentModeScaleAspectFill;//or whichever mode works best for you
[yourBaseButton addSubview:yourPlusSign];
如果你使用像我這樣的字體圖像,你可以使用NSParagraphStyle做到這一點
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSTextAlignmentCenter];
UIFont *font1 = [UIFont fontWithName:@"Avenir-Roman" size:14.0];
UIFont *font2 = [UIFont fontWithName:@"Icon-Moon" size:12.0];
NSDictionary *fontSyle1 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleNone),
NSFontAttributeName:font1,
NSParagraphStyleAttributeName:style};
NSDictionary *fontStyle2 = @{NSUnderlineStyleAttributeName:@(NSUnderlineStyleNone),
NSFontAttributeName:font2,
NSParagraphStyleAttributeName:style};
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:[IcoMoon iconString:k12_ADD_NOTE] attributes:fontStyle2]];
[attString appendAttributedString:[[NSAttributedString alloc] initWithString:@" Add Note" attributes:fontStyle1]];
[_addNewBtn setAttributedTitle:attString forState:UIControlStateNormal];
[[_addNewBtn titleLabel] setNumberOfLines:0];
_addNewBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
_addNewBtn.layer.borderWidth = 1;
[_addNewBtn setBackgroundColor:[UIColor whiteColor]];
[_addNewBtn setTintColor:[UIColor darkGrayColor]];
這那裏你可以得到圖標字體https://icomoon.io/
這部分的方式轉換成部分燒焦的爲字符串
[IcoMoon iconString:k12_ADD_NOTE]
你是什麼意思查出通過檢索文本的位置。 –
好吧,如果我可以檢索到第一個字符的x位置,那麼我只是在圖像前面恰當地放置圖像。 –
你可以繼承uibutton並在其上添加uiimageview和uilabel以獲得文本和所有屬性,你也可以自定義它。 –