【Cocoa】cocoa UI 常用算法
来源:互联网 发布:淘宝疯马皮哪家好 编辑:程序博客网 时间:2024/05/22 09:41
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; //NSRangetextRange; 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
- 【Cocoa】cocoa UI 常用算法
- Cocoa
- Cocoa
- Cocoa
- Cocoa
- Mac Cocoa UI Api
- UI - Cocoa Touch框架
- CoCoa+常用代码
- 常用Cocoa框架概览
- Cocoa设计模式(iOS常用设计模式) Cocoa Design Patterns
- Cocoa与Cocoa Touch
- Cocoa/Cocoa Touch
- Cocoa中常用绘制方法实例代码
- cocoa 几个常用的图片处理函数
- cocoa 几个常用的图片处理函数
- Cocoa Foundation框架中的一些常用类
- 常用的cocoa GitHub开源代码
- Cocoa Hack
- 谷歌浏览器的一个新特点—关于获取iframe的parent对象
- 安装 qwt6
- 如何将Emmet安装到到 Sublime text 3?
- 在互联网公司大举进军移动支付和网络理财之际 平安推出壹钱包对战互联网“群狼”
- Unity3D调用Android接口
- 【Cocoa】cocoa UI 常用算法
- 微软谷歌面试100题之调整数组顺序使奇数位于偶数前面
- XMPP协议实现原理介绍
- loadrunner自带例子r打不开IE
- android ndk 生成so
- C++ Primer Plus的string类的简单实现
- c++获取程序路径
- Linux删除文件夹命令
- vim bundle configuration