【Cocoa】cocoa UI 常用算法

10 篇文章 0 订阅

1. 计算字符串高度

- (void)resizeTextField:(NSTextField *)textField withAttributeString:(NSAttributedString *)attrStr andFixedWidth:(float)fixedWidth
{
    [textField setAttributedStringValue:attrStr];
    
    NSTextFieldCell *cell = [textField cell];
    [textField setStringValue:[attrStr string]];
    float textFieldHeight = [cell cellSizeForBounds:NSMakeRect(0, 0, fixedWidth, FLT_MAX)].height;
    [textField setStringValue:@""];
    
    textField.frame = NSMakeRect(textField.frame.origin.x, textField.frame.origin.y, fixedWidth, textFieldHeight);
}

-(void)setWarningText:(NSString*)text
{
    [textField setStringValue:text];
    NSSize textSize = NSZeroSize;
    if (textField.stringValue.length > 0) {
        NSRange range;
        NSDictionary* atribute = [[textField attributedStringValue] attributesAtIndex:0 effectiveRange:&range];
        
        textSize = [textField.stringValue  boundingRectWithSize:NSMakeSize(textField.frame.size.width, 600) options:NSStringDrawingUsesLineFragmentOrigin attributes:atribute].size;
    }
    
    NSRect frame = self.frame;
    int textHeight = (((textSize.height == 0) ? TOOL_SIZE : textSize.height)+ DELOREAN_GAP * 2);
    frame.origin.y += frame.size.height - textHeight;
    frame.size.height = textHeight;
    self.frame = frame;

}

[注]boundingRectWithSize:NSMakeSize(textField.frame.size.width, 600) 中600是随意的数字,最高不超过这个值


NSSize getTextLayoutByWidthAndFont(NSString* string, float width, NSFont* font)
{
    NSSize containerSize;
    containerSize.width = width;
    containerSize.height = FLT_MAX;
    
    //NSRange	textRange;
    NSTextStorage* textStorage = [[NSTextStorage alloc] init];
    NSTextContainer* textContainer = [[NSTextContainer alloc] initWithContainerSize:containerSize];
    NSLayoutManager* textLayoutManager = [[NSLayoutManager alloc] init];
    
    [textLayoutManager addTextContainer:textContainer];
    [textStorage addLayoutManager:textLayoutManager];
    [textContainer setLineFragmentPadding:0.0f];
    NSDictionary *styles = [[[NSDictionary alloc] initWithObjectsAndKeys:
                                       font, NSFontAttributeName,
                                       [NSColor blackColor], NSForegroundColorAttributeName,
                                       nil] autorelease];
    
    
    [[textStorage mutableString] setString:string];
    [textStorage setAttributes:styles range:NSMakeRange(0U, [string length])];

    [textLayoutManager glyphRangeForTextContainer:textContainer];
    
    containerSize.height = [textLayoutManager usedRectForTextContainer:textContainer].size.height;
    [textContainer release];
    [textStorage release];
    [textLayoutManager release];
    
    NSAttributedString* title = [[[NSAttributedString alloc] initWithString:string  attributes:styles] autorelease];
    containerSize.width = containerSize.width < title.size.width?containerSize.width:title.size.width+2;
    return containerSize;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值