设置行间距,计算带行间距的文本高度

时间:2022-09-03 06:10:23

设置行间距,计算带行间距的文本高度

//求有行间距的label高度
- (CGSize)boundingRectWithSize:(CGSize)size WithStr:(NSString*)string andFont:(UIFont *)font andLinespace:(CGFloat)space
{

NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
[style setLineSpacing:space];
NSDictionary *attribute = @{NSFontAttributeName:font,NSParagraphStyleAttributeName:style};
CGSize retSize = [string boundingRectWithSize:size
options:\
NSStringDrawingTruncatesLastVisibleLine |
NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesFontLeading
attributes:attribute
context:nil].size;

return retSize;
}

//设置label颜色,字体大小,行间距等
-(void)labelAtrributedSetBy:(UILabel*)label andIndex:(NSInteger)index andFont:(UIFont*)font andColor1:(UIColor*)color1 andColor2:(UIColor*)color2 andLength:(NSString*)str{
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:label.text];
NSRange range = NSMakeRange(index,str.length);
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
[style setLineSpacing:4];

NSDictionary * adic = @{NSFontAttributeName :font,NSForegroundColorAttributeName:color1,NSParagraphStyleAttributeName:style};
[AttributedStr addAttributes:adic range:NSMakeRange(0, index)];

NSDictionary * bdic = @{NSFontAttributeName :font,NSForegroundColorAttributeName:color2,NSParagraphStyleAttributeName:style};
[AttributedStr addAttributes:bdic range:range];
label.attributedText = AttributedStr;
}


NSMutableAttributedString  <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">富文本的一些属性</span>

 1 @property(readwrite) CGFloat lineSpacing;              //行间距
2 @property(readwrite) CGFloat paragraphSpacing;           //段间距
3 @property(readwrite) NSTextAlignment alignment;           //对齐方式
4 @property(readwrite) CGFloat firstLineHeadIndent;          //首行缩紧
5 @property(readwrite) CGFloat headIndent;               //除首行之外其他行缩进
6 @property(readwrite) CGFloat tailIndent;               //每行容纳字符的宽度
7 @property(readwrite) NSLineBreakMode lineBreakMode;        //换行方式
8 @property(readwrite) CGFloat minimumLineHeight;           //最小行高
9 @property(readwrite) CGFloat maximumLineHeight;           //最大行高
10 @property(readwrite) NSWritingDirection baseWritingDirection;  //书写方式(NSWritingDirectionNaturalNSWritingDirectionLeftToRightNSWritingDirectionRightToLeft
11 @property(readwrite) CGFloat lineHeightMultiple;
12 @property(readwrite) CGFloat paragraphSpacingBefore;
13 @property(readwrite) float hyphenationFactor;
14 @property(readwrite,copy,NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE_IOS(7_0);
15 @property(readwrite,NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE_IOS(7_0);

NSForegroundColorAttributeName    文字前景色

NSBackgroundColorAttributeName     文字背景色

NSLigatureAttributeName        连体字(NSNumber  @0:无连体,@1:默认连体,系统字体不包含对连体的支持)

NSUnderlineStyleAttributeName     下划线

NSStrokeColorAttributeName       只有在NSStrokeWidthAttributeName设置了值之后才有效(默认字体颜色和前景色一致,如果设置的颜色和前景色不一致则前景色无效)

NSStrokeWidthAttributeName      设置该属性之后字体变成空心字体,字体边线宽度为value设定的值

NSBaselineOffsetAttributeName     值为NSNumber类型,表明文字相对于其他文字基准线向上的偏移量

NSUnderlineColorAttributeName      值为UIColor类型,下划线颜色(只有在NSUnderlineStyleAttributeName的value为@1时有效)

NSUnderlineStyleAttributeName      值为NSNumber类型,下划线宽度(默认值为@0:下划线宽度为0——不现实下划线,@1:字符串有下划线)


方法2:KVC

//    [self.writeTextField setValue:c4 forKeyPath:@"_placeholderLabel.textColor"];
// [self.writeTextField setValue:H7 forKeyPath:@"_placeholderLabel.font"];
// [self.writeTextField setValue:c3 forKeyPath:@"_placeholderLabel.backgroundColor"];



设置placeholder frame

//设置placeholderRect
- (void)drawPlaceholderInRect:(CGRect)rect{
UIColor *placeholderColor = [UIColor redColor];//设置颜色
[placeholderColor setFill];

// CGRect placeholderRect = CGRectMake(rect.origin.x+10, (rect.size.height- H7.pointSize)/2, rect.size.width, H7.pointSize);//设置距离
CGRect placeholderRect = rect;
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineBreakMode = NSLineBreakByTruncatingTail;
style.alignment = self.writeTextField.textAlignment;

NSDictionary *attr = @{NSParagraphStyleAttributeName:style,NSFontAttributeName:H3,NSForegroundColorAttributeName:c4};

[self.writeTextField.placeholder drawInRect:placeholderRect withAttributes:attr];
}