Button / Label 橫向圖文混排
隨筆而已 ,自己做個(gè)記錄
此方法封裝樣式是(文字 圖片 文字) 返回attributedString 類型,所以button 和 Label 通用,圖片固定被寫死,可加傳參
-(NSMutableAttributedString*)getBtnAttributedString:(NSString*)attriStr lastAttri:(NSString*)attLast
{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?///創(chuàng)建第一段富文本 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?NSMutableAttributedString*attri =[[NSMutableAttributedString alloc]initWithString:attriStr]; ? ? ?/// 創(chuàng)建最后一段富文本 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSMutableAttributedString*attriLast = ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[[NSMutableAttributedString alloc]initWithString:attLast];?
///創(chuàng)建中間圖片富文本 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSTextAttachment*attch = [[NSTextAttachment alloc]init]; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? attch.image= [UIImage imageNamed:@"ic_sx"]; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ///圖片占位大小 也可用image.size屬性 自動(dòng)計(jì)算 ? ? ? ? ? ? ? ? ?attch.bounds=CGRectMake(0,5,37/2,11/2); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSAttributedString*string = [NSAttributedStringattributedStringWithAttachment:attch];
/// 加入圖片 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [attri appendAttributedString:string];
///加入最后一段文字 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [attri appendAttributedString:attriLast];
[attri addAttribute:NSForegroundColor AttributeNamevalue:UIColorFromRGB(0x4083F5) range:NSMakeRange(0,3)];
[attri addAttribute:NSForegroundColor AttributeNamevalue:UIColorFromRGB(0x4083F5) range:NSMakeRange(attri.length-1,1)];
[attri addAttribute:NSFontAttributeNamevalue:Font_Title16 range:NSMakeRange(0,3)];
[attri addAttribute:NSFontAttributeNamevalue:Font_Title16 range:NSMakeRange(attri.length-1,1)];
return attri ;
}
[btn setAttributedTitle:[self getBtnAttributedString:strAttri lastAttri:strAttriLast] forState:UIControlStateNormal];
還有一種是要計(jì)算文字和圖片的寬高,修改btn.imageEdgeInsets 和 btn.imageEdgeInsets 的值來排版文字和圖片,網(wǎng)上很多,不做贅述,想寫再寫。
最后祝自己越來越帥,白富美向我表白