在iOS7中,sizeWithFont不推荐使用,因此我正在使用boundingRectWithSize(返回CGRect值).我的代码:
UIFont *fontText = [UIFont fontWithName:[AppHandlers zHandler].fontName size:16];
// you can use your font.
CGSize maximumLabelSize = CGSizeMake(310, 9999);
CGRect textRect = [myString boundingRectWithSize:maximumLabelSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName:fontText}
context:nil];
expectedLabelSize = CGSizeMake(textRect.size.width, textRect.size.height);
Run Code Online (Sandbox Code Playgroud)
在textRect,我的尺寸大于我maximumLabelSize,尺寸与使用时不同sizeWithFont.我该如何解决这个问题?
我想在固定宽度的框中绘制一些NSAttributedStrings,但是在计算绘制它们时会占用的正确高度时遇到问题.到目前为止,我已经尝试过:
调用- (NSSize) size,但结果是无用的(为此目的),因为他们会给出字符串所需的宽度.
- (void)drawWithRect:(NSRect)rect options:(NSStringDrawingOptions)options使用矩形调用我想要的宽度和NSStringDrawingUsesLineFragmentOrigin选项,就像我在绘图中使用的那样.结果......难以理解; 当然不是我想要的.(正如许多地方所指出的,包括这个 Cocoa-Dev线程).
创建一个临时的NSTextView并执行:
当我查询tmpView的框架时,宽度仍然是所需的,并且高度通常是正确的......直到我得到更长的字符串,当它通常是所需尺寸的一半时.(似乎没有最大尺寸被击中:一帧将是273.0高(约300太短),另一帧将是478.0(只有60-ish太短)).
[[tmpView textStorage] setAttributedString:aString];
[tmpView setHorizontallyResizable:NO];
[tmpView sizeToFit];
如果有人管理过这个,我会感激任何指点.
我有使用XIB而没有自动布局的应用程序的问题.我不知道这是否是重要信息.
我有UILabel有两行使用自动换行.在iOS 10中,自动换行正常工作,第一行包含一个单词+特殊字符,例如&符号.例:
然后在iOS 11上,自动换行错误并将放大器放到第二行:
这是有问题的,因为通常在第二行上安装的较长的单词现在未正确显示.知道改变了什么吗?我知道safeArea但它看起来不像是理由.任何想法如何将该&符号移到顶部哪里有足够的空间?
当使用[NSString boundingRectWithSize:options:attributes]返回的rect的大小比我期望的某些字符串高.返回的高度似乎表示具有给定属性的字符串的最大可能高度,而不是字符串本身的高度.
假设相同的属性和选项,字符串" cars" 返回的高度与字符串" " 返回的高度相同ÉTAS-UNIS(注意E上的重音).
我本来希望boundingRectWithSize只考虑给定字符串中的字符,在我看来,它会为字符串" cars" 返回一个较短的高度.
在附带的截图中,我填写了返回的矩形,boundingRectWithSize并用红色概述了我认为边界矩形应该是什么.矩形的宽度与我预期的差不多,但高度比我预期的要高得多.这是为什么?

示例代码:
NSRect boundingRect = NSZeroRect;
NSSize constraintSize = NSMakeSize(CGFLOAT_MAX, 0);
NSString *lowercaseString = @"cars";
NSString *uppercaseString = @"ÉTAS-UNIS";
NSString *capitalizedString = @"Japan";
NSFont *drawingFont = [NSFont fontWithName:@"Georgia" size:24.0];
NSDictionary *attributes = @{NSFontAttributeName : drawingFont, NSForegroundColorAttributeName : [NSColor blackColor]};
boundingRect = [lowercaseString boundingRectWithSize:constraintSize options:0 attributes:attributes];
NSLog(@"Lowercase rect: %@", NSStringFromRect(boundingRect));
boundingRect = [uppercaseString boundingRectWithSize:constraintSize options:0 attributes:attributes];
NSLog(@"Uppercase rect: %@", NSStringFromRect(boundingRect));
boundingRect …Run Code Online (Sandbox Code Playgroud) 如何添加填充NSTableCellView?
我希望内容的填充量为10px,类似于纯HTML中的内容.(所有面).文本应在中间垂直居中.
目前我正在NSTextFieldCell的子类中执行此操作.但这似乎不正常.
编辑文本时,编辑文本字段不使用textfieldcell中的填充.
图2:


这是我目前的代码,(子类NSTextFieldCell)
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSRect titleRect = [self titleRectForBounds:cellFrame];
NSAttributedString *aTitle = [self attributedStringValue];
if ([aTitle length] > 0) {
[aTitle drawInRect:titleRect];
}
}
- (NSRect)titleRectForBounds:(NSRect)bounds
{
NSRect titleRect = bounds;
titleRect.origin.x += 10;
titleRect.origin.y = 2;
NSAttributedString *title = [self attributedStringValue];
if (title) {
titleRect.size = [title size];
} else {
titleRect.size = NSZeroSize;
}
// We don't want the width of the string going outside the cell's …Run Code Online (Sandbox Code Playgroud) [text boundingRectWithSize:BOLIVIASize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FONT} context:nil];
Run Code Online (Sandbox Code Playgroud)
这是新的替代品
- (CGSize) sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode) lineBreakMode
Run Code Online (Sandbox Code Playgroud)
但是,如何在boundingRectWithSize上指定lineBreakMode参数?
objective-c ×4
cocoa ×3
ios ×3
nsstring ×2
uilabel ×2
appkit ×1
ios11 ×1
ios7 ×1
iphone ×1
macos ×1
nstableview ×1
nstextview ×1
xcode ×1
xcode4.5 ×1