Button / Label 橫向圖文混排

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)上很多,不做贅述,想寫再寫。

最后祝自己越來越帥,白富美向我表白

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容