ios顯示html標籤,超連結顏色以及下劃線的處理
有的時候,我們的app需要顯示html標籤的效果,這個時候,如果需求不是特別複雜的話,我們都是可以使用textView來實現的。對冉Label也可以實現,但是label很難實現超連結的點選效果,所以,還是建議使用textView。
//modelData.content的資料其中一個是這種結構:
//<a href="juzi://article/detail/native?id=37352">橘子君請大家參加party啦,等大家來玩,點我報名,快來和橘子君一起玩耍吧!</a>
NSString * htmlString = modelData.content;
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
self.textView.attributedText = attributedString;
//設定textView的高度,這裡是根據文字計算的textView的高度
self.textViewHeight.constant = [PublicTool normalFontheightWithWidth:mScreenWidth - 24 andString:[attributedString string] andFontSize:14]+15;
self.textView.linkTextAttributes = @{
NSForegroundColorAttributeName:[UIColor colorFromRGB:0xfc5f59],//設定超連結顏色
NSUnderlineStyleAttributeName:@(NSUnderlineStyleNone),//設定超連結的下劃線為none,但是在ios10上無效,自己測試的無效,原因沒找到呢
NSUnderlineColorAttributeName:[UIColor whiteColor]//無奈,只得除此下策,改變下劃線顏色為白色,因為背景是白色,所以就隱藏了下劃線
};//設定連結的文字顏色
normalFontheightWithWidth方法如下:(供參考)
//根據文字,寬度,字型大小計算高度
+(CGFloat)normalFontheightWithWidth:(CGFloat)width andString:(NSString *)str andFontSize:(int)fontSize{
CGSize titleSize = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil].size;
return titleSize.height ;
}
附超連結的屬性:
/************************ Attributes ************************/
// Predefined character attributes for text. If the key is not in the dictionary, then use the default values as described below.
UIKIT_EXTERN NSString * const NSFontAttributeName NS_AVAILABLE(10_0, 6_0); // UIFont, default Helvetica(Neue) 12
UIKIT_EXTERN NSString * const NSParagraphStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSParagraphStyle, default defaultParagraphStyle
UIKIT_EXTERN NSString * const NSForegroundColorAttributeName NS_AVAILABLE(10_0, 6_0); // UIColor, default blackColor
UIKIT_EXTERN NSString * const NSBackgroundColorAttributeName NS_AVAILABLE(10_0, 6_0); // UIColor, default nil: no background
UIKIT_EXTERN NSString * const NSLigatureAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing integer, default 1: default ligatures, 0: no ligatures
UIKIT_EXTERN NSString * const NSKernAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled.
UIKIT_EXTERN NSString * const NSStrikethroughStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing integer, default 0: no strikethrough
UIKIT_EXTERN NSString * const NSUnderlineStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing integer, default 0: no underline
UIKIT_EXTERN NSString * const NSStrokeColorAttributeName NS_AVAILABLE(10_0, 6_0); // UIColor, default nil: same as foreground color
UIKIT_EXTERN NSString * const NSStrokeWidthAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)
UIKIT_EXTERN NSString * const NSShadowAttributeName NS_AVAILABLE(10_0, 6_0); // NSShadow, default nil: no shadow
UIKIT_EXTERN NSString *const NSTextEffectAttributeName NS_AVAILABLE(10_10, 7_0); // NSString, default nil: no text effect
UIKIT_EXTERN NSString * const NSAttachmentAttributeName NS_AVAILABLE(10_0, 7_0); // NSTextAttachment, default nil
UIKIT_EXTERN NSString * const NSLinkAttributeName NS_AVAILABLE(10_0, 7_0); // NSURL (preferred) or NSString
UIKIT_EXTERN NSString * const NSBaselineOffsetAttributeName NS_AVAILABLE(10_0, 7_0); // NSNumber containing floating point value, in points; offset from baseline, default 0
UIKIT_EXTERN NSString * const NSUnderlineColorAttributeName NS_AVAILABLE(10_0, 7_0); // UIColor, default nil: same as foreground color
UIKIT_EXTERN NSString * const NSStrikethroughColorAttributeName NS_AVAILABLE(10_0, 7_0); // UIColor, default nil: same as foreground color
UIKIT_EXTERN NSString * const NSObliquenessAttributeName NS_AVAILABLE(10_0, 7_0); // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew
UIKIT_EXTERN NSString * const NSExpansionAttributeName NS_AVAILABLE(10_0, 7_0); // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion
UIKIT_EXTERN NSString * const NSWritingDirectionAttributeName NS_AVAILABLE(10_6, 7_0); // NSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters. The control characters can be obtained by masking NSWritingDirection and NSWritingDirectionFormatType values. LRE: NSWritingDirectionLeftToRight|NSWritingDirectionEmbedding, RLE: NSWritingDirectionRightToLeft|NSWritingDirectionEmbedding, LRO: NSWritingDirectionLeftToRight|NSWritingDirectionOverride, RLO: NSWritingDirectionRightToLeft|NSWritingDirectionOverride,
UIKIT_EXTERN NSString * const NSVerticalGlyphFormAttributeName NS_AVAILABLE(10_7, 6_0); // An NSNumber containing an integer value. 0 means horizontal text. 1 indicates vertical text. If not specified, it could follow higher-level vertical orientation settings. Currently on iOS, it`s always horizontal. The behavior for any other value is undefined.
加油吧!
相關文章
- html中常用的標籤-超連結標籤HTML
- iOS UILabel顯示html標籤iOSUIHTML
- html中a標籤的下劃線怎麼去掉HTML
- HTML常用基礎標籤:圖片與超連結標籤全解!HTML
- word空格處不顯示下劃線
- HTML簡介,結構,標籤以及標籤語義HTML
- android更改EditText下劃線顏色Android
- ReSharper 顯示使用的顏色
- 標籤的顯示模式模式
- 【Python】正規表示式過濾文字中的html標籤、url超連結、img連結PythonHTML
- CSS 多行文字超連結下劃線動效CSS
- C#裡面超連結標籤的使用C#
- Android中顯示html標籤或者帶圖片AndroidHTML
- HTML樣式插入、連結、表單標籤HTML
- HTML 顏色HTML
- ReactNative IOS下Image標籤載入網路圖片不顯示ReactiOS
- linux中顯示有顏色的字元......Linux字元
- shell日誌顏色處理
- HTML 顏色色號HTML
- Excel不顯示網格線、更改網格線顏色、列印網格線Excel
- HTML <hr>水平線標籤HTML
- HTML <hr> 水平線標籤HTML
- ECHARTS-折線圖不顯示資料 控制折線圖顏色Echarts
- 【動畫進階】單標籤下多色塊隨機文字隨機顏色動畫動畫隨機
- linux下目錄、檔案顯示顏色的設定生效Linux
- 直播系統平臺搭建,管理下劃線顏色、高度設定標題是否居中
- 【Ansible】Ansible 連線主機顯示報錯的處理方案
- UITabBarController、TabBar背景顏色設定、TabBarItem顏色處理UItabBarController
- HTML超連結HTML
- Word下劃線不顯示解決方法
- CSS 控制 input 只顯示下劃線效果CSS
- HTML 關於標籤,虛線HTML
- Mac 終端命令列顏色高亮顯示Mac命令列
- iPhone中XML處理以及網路上的圖片顯示iPhoneXML
- CSS動畫:有活力的連結下劃線CSS動畫
- HTML標籤(基本標籤的使用)HTML
- 常見的linux的檔案顯示的顏色Linux
- QT設定標籤顯示位置QT