iOS 7 之后字体设置新方法

//
//  VerticalAlign.m
//  test_ Input_01
//
//  Created by admin on 4/22/16.
//  Copyright © 2016 jeffasd. All rights reserved.
//

#import "VerticalAlign.h"

@implementation VerticalAlign

- (void)alignTop {
//    CGSize fontSize = [self.text sizeWithFont:self.font];
    
    NSDictionary *attributes = @{NSFontAttributeName : self.font};
    CGSize fontSize = [self.text sizeWithAttributes:attributes];
    
    double finalHeight = fontSize.height * self.numberOfLines;
    double finalWidth = self.frame.size.width;    //expected width of label
//    CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
    
    
    
    //设置段落模式
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.lineBreakMode = self.lineBreakMode;
    NSDictionary *attribute = @{NSFontAttributeName: self.font, NSParagraphStyleAttributeName: paragraph};
    
    CGSize theStringSize = [self.text boundingRectWithSize:CGSizeMake(finalWidth, finalHeight) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    
    int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;
    for(int i=0; i<newLinesToPad; i++)
        self.text = [self.text stringByAppendingString:@"\n "];
}

- (void)alignBottom {
//    CGSize fontSize = [self.text sizeWithFont:self.font];
    
    NSDictionary *attributes = @{NSFontAttributeName : self.font};
    CGSize fontSize = [self.text sizeWithAttributes:attributes];
    
    double finalHeight = fontSize.height * self.numberOfLines;
    double finalWidth = self.frame.size.width;    //expected width of label
    
    
//    CGSize theStringSize = [self.text sizeWithFont:self.font constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];
    
    //设置段落模式
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.lineBreakMode = self.lineBreakMode;
    NSDictionary *attribute = @{NSFontAttributeName: self.font, NSParagraphStyleAttributeName: paragraph};
    CGSize theStringSize = [self.text boundingRectWithSize:CGSizeMake(finalWidth, finalHeight) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    
    int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;
    for(int i=0; i<newLinesToPad; i++)
        self.text = [NSString stringWithFormat:@" \n%@",self.text];
}

@end


//label 字体设置

    NSString *text = @"attributedString测试:行间距为8。iOS6之后新属性测试";
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:30];
    UIColor *color = [UIColor blackColor];
    NSAttributedString *string = [[NSAttributedString alloc] initWithString:text attributes:@{NSForegroundColorAttributeName : color, NSParagraphStyleAttributeName: paragraphStyle}];
    
    
    UILabel *label = [UILabel new];
    [self.view addSubview:label];
    label.attributedText = string;

http://stackoverflow.com/questions/21654671/sizewithfont-constrainedtosize-linebreakmode-method-is-deprecated-in-ios-7

sizeWithFont: ConstrainedToSize: lineBreakMode: 方法弃用 iOS 7 中和我有点不确定如何处理这到底。后一点的互联网找到了是一种新方法来处理这问题的研究,即:

[txt drawWithRect: options: attributes: context:]

这是我目前正试图运行的方法:

+ (CGSize)textSizeForText:(NSString *)txt
{
   
CGFloat width = [UIScreen mainScreen].applicationFrame.size.width * 0.75f;
   
CGFloat height = MAX([JSBubbleView numberOfLinesForMessage:txt],
                         
[txt numberOfLines]) * [JSMessageInputView textViewLineHeight];


   
return [txt sizeWithFont:[JSBubbleView font]
           constrainedToSize
:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
               lineBreakMode
:NSLineBreakByWordWrapping];

}

但我却很难将其转换为新的方法。主要是与 lineBreakMode: 这是无处可在新方法中。有什么想法?

解决方法 1:

在新方法中,为换行,您必须首先创建一个 NSMutableParagraphStyle 样式:

  NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
  paragraphStyle
.lineBreakMode = NSLineBreakByWordWrapping;

然后通过传递所有必要的参数只是使用的新方法

  CGRect textRect = [text boundingRectWithSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
                                      options
:NSStringDrawingUsesLineFragmentOrigin
                                   attributes
:@{NSParagraphStyleAttributeName: paragraphStyle.copy}
                                      context
:nil];

 
return textRect.size;

如果你想这是潮流,你可以做

  return ([text boundingRectWithSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
                                      options
:NSStringDrawingUsesLineFragmentOrigin
                                   attributes
:@{NSParagraphStyleAttributeName: paragraphStyle.copy}
                                      context
:nil]).size;

希望有所帮助


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值