//
// 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;
希望有所帮助