效果圖
- (void)showCity:(NSString *)cityname
{
//實現(xiàn)富文本
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:cityname attributes:nil];
//進行圖文混排
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
textAttachment.image = [UIImage imageNamed:@"f_flagdown02@2x"];
textAttachment.bounds =CGRectMake(0,0, 11,8);
NSAttributedString * textAttachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
//判斷選擇的城市名稱長度是否大于5個字符的長度晦譬,如果大于五個字符就取前四個字符,然后再在最后添加一個圖標(biāo)
if (string.length >=5) {
//去前四個字符
NSAttributedString *str = [string attributedSubstringFromRange:NSMakeRange(0,4)];
//將NSAttributedString類型轉(zhuǎn)換成NSString類型
NSString *cityString = [str string];
string = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@..",cityString] attributes:nil];
}
//在城市名稱后插入圖片
[string insertAttributedString:textAttachmentString atIndex:string.length];
//自定義按鈕
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame =CGRectMake(0,0, 70,30);
[btn addTarget:self action:@selector(navBtnCLick)forControlEvents:UIControlEventTouchUpInside];
UILabel *lbText = [[UILabel alloc] initWithFrame:CGRectMake(0,0, btn.frame.size.width+10, btn.frame.size.height)];
lbText.attributedText = string;
lbText.textColor = [UIColorwhite Color];
lbText.font = [UIFont systemFontOfSize:13];
[btn addSubview:lbText];
btn.backgroundColor = [UIColor clearColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者