UITextView富文本實現(xiàn)跳轉(zhuǎn)鏈接并實現(xiàn)文本居中-- 服務(wù)協(xié)議、隱私條款

實現(xiàn)效果


注冊登錄

UITextView *protocolTV = [[UITextView alloc] initWithFrame:CGRectZero];

protocolTV.editable=NO;

protocolTV.delegate=self;

NSMutableParagraphStyle *paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

//設(shè)置text文字居中

paragraph.alignment=NSTextAlignmentCenter;

protocolTV.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);

//設(shè)置添加鏈接部分文字的顏色

protocolTV.linkTextAttributes = @{NSForegroundColorAttributeName:TextBodyColor};

[wechatView addSubview:protocolTV];


NSString *str = @"注冊登錄即代表同意XX的服務(wù)協(xié)議和隱私條款";

NSRangeprotocalRange = [strrangeOfString:@"服務(wù)協(xié)議"];

NSRangeprivacyRange = [strrangeOfString:@"隱私條款"];

NSMutableAttributedString *privacyMutableAttrStr = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFang SC" size: 12],NSForegroundColorAttributeName:TextBodyColor,NSParagraphStyleAttributeName:paragraph}];


//給需要 點擊的部分添加鏈接

[privacyMutableAttrStr addAttributes:@{NSLinkAttributeName:@"privacy://",NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Semibold" size: 12],NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)} range:privacyRange];

[privacyMutableAttrStr addAttributes:@{NSLinkAttributeName:@"protocal://",NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Semibold" size: 12],NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle)} range:protocalRange];

protocolTV.attributedText= privacyMutableAttrStr;

[protocolTV mas_makeConstraints:^(MASConstraintMaker*make) {

? ? ? ? ? ?make.top.equalTo(self.wechatBtn.mas_bottom).offset(24);

? ? ? ? ? ?make.centerX.equalTo(bgview.mas_centerX);

? ? ? ? ? ?make.left.right.equalTo(bgview);

? ? ? ? ? ?make.height.mas_equalTo(15);

? ? ? ? ? ?make.width.equalTo(bgview.mas_width);

?}];


#pragma mark - UITextViewDelegate

-(BOOL)textView:(UITextView*)textViewshouldInteractWithURL:(NSURL*)URLinRange:(NSRange)characterRangeinteraction:(UITextItemInteraction)interaction{

? ? if ([URL.scheme isEqualToString:@"privacy"]) {

//這里調(diào)用方法跳到隱私條款頁面

? ? ? ? returnNO;

? ? }

? ? if ([URL.scheme isEqualToString:@"protocal"]) {

//這里調(diào)用方法跳到服務(wù)協(xié)議頁面

? ? ? ? returnNO;

? ? }

? ??return YES;

}



其中也有遇到問題:UITextView文本內(nèi)容居中

需要為文本設(shè)置NSMutableParagraphStyle

NSMutableParagraphStyle *paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

//設(shè)置text文字居中

paragraph.alignment=NSTextAlignmentCenter;

再設(shè)置NSMutableAttributedString

NSParagraphStyleAttributeName:paragraph ? ?設(shè)置段落樣式

此解決方案來自iOS — NSMutableAttributedString和NSMutableParagraphStyle

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

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

  • (一)Masonry介紹 Masonry是一個輕量級的布局框架 擁有自己的描述語法 采用更優(yōu)雅的鏈?zhǔn)秸Z法封裝自動布...
    木易林1閱讀 2,576評論 0 3
  • Masonry是一個輕量級的布局框架,擁有自己的描述語法,采用更優(yōu)雅的鏈?zhǔn)秸Z法封裝自動布局,簡潔明了并具有高可讀性...
    3dcc6cf93bb5閱讀 1,934評論 0 1
  • 字體屬性設(shè)置示例:if(color ==nil) {color = [NSColor redColor];}NSF...
    袏扌戒指閱讀 8,504評論 0 2
  • 一、關(guān)于AttributedString 在iOS開發(fā)過程中系統(tǒng)簡單的封裝工具已經(jīng)不夠我們使用,為了更好的開發(fā),蘋...
    iceMaple閱讀 7,103評論 2 31
  • 與NSString類似,在iOS中AttributedString也分為NSAttributedString和 N...
    錢十六閱讀 912評論 0 0

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