iOS随笔小记--富文本添加超链接
2018-05-23 本文已影响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;
}