美文网首页
iOS随笔小记--富文本添加超链接

iOS随笔小记--富文本添加超链接

作者: 七一小月 | 来源:发表于2018-05-23 10:17 被阅读25次
    UITextView * conditionsContentTextView;
    

    uitextview,富文本添加超链接

     UIFont * textFont;
      if ([SDiPhoneVersion deviceSize] == iPhone35inch || [SDiPhoneVersion deviceSize] == iPhone4inch) {
            textFont = k_Helvetica_15;
        } else {
            textFont = k_Helvetica_16;
        }
       NSDictionary * dictionary = @{NSFontAttributeName:textFont,NSForegroundColorAttributeName:k_fontColor};
       NSString * string = NSLocalizedString(@"I accept SALUS Controls Terms & Conditions", nil);
    NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:string attributes:dictionary];
    [attributeStr addAttribute:NSLinkAttributeName value:@"https://salus-it500.com/public/licence_agreement.php" range:[string rangeOfString:NSLocalizedString(@"Terms & Conditions", nil)]];
    
      self.conditionsContentTextView.attributedText = attributeStr;
      self.conditionsContentTextView.editable = NO;
      self.conditionsContentTextView.scrollEnabled = NO;
      self.heightOfconditionsTextView.constant = [self heightForString:string fontSize:textFont andWidth:CGRectGetWidth(self.conditionsContentTextView.frame)];
    

    计算textview的高度

    - (float) heightForString:(NSString *)value fontSize:(UIFont *)fontSize andWidth:(float)width{
        UITextView *detailTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, width, 0)];
        detailTextView.font = fontSize;
        detailTextView.text = value;
        CGSize deSize = [detailTextView sizeThatFits:CGSizeMake(width,CGFLOAT_MAX)];
        return deSize.height;
    }

    相关文章

      网友评论

          本文标题:iOS随笔小记--富文本添加超链接

          本文链接:https://www.haomeiwen.com/subject/peoljftx.html