iOS关于指定字体及两端对齐的富文本


2.首先了解一下有关富文本的相关属性:

pragma mark ---相关属性-

/*//NSFontAttributeName   字号 UIFont 默认12
 //NSParagraphStyleAttributeName    段落样式  NSParagraphStyle
 //NSForegroundColorAttributeName    前景色   UIColor
 //NSBackgroundColorAttributeName    背景色   UIColor
 //NSObliquenessAttributeName        字体倾斜     NSNumber
 //NSExpansionAttributeName          字体加粗     NSNumber  比例 0就是不变 1增加一倍
 //NSKernAttributeName               字间距   CGFloat
 //NSUnderlineStyleAttributeName     下划线     10
 //NSUnderlineColorAttributeName     下划线颜色
 //NSStrikethroughStyleAttributeName 删除线   10
 //NSStrikethroughColorAttributeName 某种颜色
 //NSStrokeColorAttributeName        same as ForegroundColor
 //NSStrokeWidthAttributeName        CGFloat
 //NSLigatureAttributeName           连笔字  10  没看出效果
 //NSShadowAttributeName             阴影    NSShawdow
 //NSTextEffectAttributeName          设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
 //NSAttachmentAttributeName         NSTextAttachment  设置文本附件,常用插入图片
 //NSLinkAttributeName               链接  NSURL (preferred) or NSString
 //NSBaselineOffsetAttributeName     基准线偏移   NSNumber
 
 //NSWritingDirectionAttributeName   文字方向     @[@(1),@(2)]  分别代表不同的文字出现方向等等,我想你一定用不到它 - -
 //NSVerticalGlyphFormAttributeName  水平或者竖直文本  1竖直 0水平 在iOS没卵用,不支持竖版

*/
3.实现代码
3.1 价格:$150

NSString *str = @"价格:$50";
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 150, 20,300, 30)];
//添加富文本属性
NSMutableAttributedString *arrString = [[NSMutableAttributedString alloc]initWithString:str];
//设置颜色
[arrString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(3, 3)];
//字体大小
[arrString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(3, 3)];
 NSRange range = NSMakeRange(0, arrString.length);
//删除线
[arrString addAttribute:NSStrikethroughStyleAttributeName value:@(1) range:range];
[arrString addAttribute:NSStrikethroughColorAttributeName value:[UIColor greenColor] range:range];

 //    label.text = str;
label.attributedText = arrString;
[self.view addSubview:label];

3.2两段对齐

@interface LQM_Label : UILabel
 // 实现iOS 左右两端对齐(NSTextAlignmentJustified是最后一行自然对齐)
- (void)labelAlightLeftAndRight;
 // 指定Label的width
- (void)labelAlightLeftAndRightWithWidth:(CGFloat)labelWidth

 
@implementation LQM_Label

-(void)labelAlightLeftAndRight
  {
     [self labelAlightLeftAndRightWithWidth:self.frame.size.width];
 }

-(void)labelAlightLeftAndRightWithWidth:(CGFloat)labelWidth
{
  //自适应高度
         CGSize textSize = [self.text boundingRectWithSize:CGSizeMake(labelWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine| NSStringDrawingUsesFontLeading  attributes:@{NSFontAttributeName :self.font} context:nil].size;
CGFloat margin = (labelWidth - textSize.width)/(self.text.length - 1);
NSNumber *number = [NSNumber numberWithFloat:margin];
NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc]initWithString:self.text];
//字间距 :NSKernAttributeName
[attribute addAttribute:NSKernAttributeName value:number range:NSMakeRange(0, self.text.length - 1)];
self.attributedText = attribute;

}

LQM_Label *lqmLabel1 = [[LQM_Label alloc]initWithFrame:CGRectMake(20,   100, 300, 30)];//300 label的宽度
 [self.view addSubview:lqmLabel1];
lqmLabel1.text = @"我是两端对齐";
[lqmLabel1 labelAlightLeftAndRightWithWidth:250];//250 对齐label的宽度

LQM_Label *lqmLabel2 = [[LQM_Label alloc]initWithFrame:CGRectMake(20, 150, 300, 30)];
[self.view addSubview:lqmLabel2];
lqmLabel2.text = @"我是两端对齐,我是两端对齐";
[lqmLabel2 labelAlightLeftAndRightWithWidth:250];

我的实践

// 根据定宽  具体字符串  生成特性字符串

- (NSMutableAttributedString *)genAttrStrWithFixedW:(CGFloat)fixedW originStr:(NSString *)originStr longestStr:(NSString *)longestStr {

    if (originStr.length > 0) {

        UIFont *font = [UIFont systemFontOfSize:14];

        NSMutableAttributedString *attrOriginStr = [self genAttrStrWithStr:originStr];

        CGFloat originStrW = [attrOriginStr boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, font.lineHeight) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.width;                                                    // 自动计算文本宽度

        // 如果不是最长字符串 则添加字间距

        if (![longestStr isEqualToString:originStr]) {

            

            if (originStr.length > 1) {

                

                // 计算字间距

                CGFloat characterSpacing = (fixedW - originStrW) / (originStr.length-1);

                // 这种写法会导致字符串末尾也有一个字间距

                //                [attDic setValue:@(characterSpacing) forKey:NSKernAttributeName];

                //                attrOriginStr = [[NSMutableAttributedString alloc] initWithString:originStr attributes:attDic];

                //字间距 :NSKernAttributeName

                [attrOriginStr addAttribute:NSKernAttributeName value:@(characterSpacing) range:NSMakeRange(0, originStr.length-1)];

                CGFloat originStrW1 = [attrOriginStr boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, font.lineHeight) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.width;

//                NSLog(@"%.2f", originStrW1);

            }

        }

        return attrOriginStr;

    }

    else {

        return nil;

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值