相关疑难解决方法(0)

使用drawInRect:withAttributes对齐文本:

在我的应用程序的iOS 5版本中,我有:

[self.text drawInRect: stringRect
             withFont: [UIFont fontWithName: @"Courier" size: kCellFontSize]
        lineBreakMode: NSLineBreakByTruncatingTail
            alignment: NSTextAlignmentRight];
Run Code Online (Sandbox Code Playgroud)

我正在升级iOS 7.上面的方法已被弃用.我现在正在使用drawInRect:withAttributes : . 的属性参数是一个NSDictionary对象.我可以得到drawInRect:withAttributes:使用这个来处理以前的字体参数:

      UIFont *font = [UIFont fontWithName: @"Courier" size: kCellFontSize];

      NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: font, NSFontAttributeName,
                                  nil];

      [self.text drawInRect: stringRect
             withAttributes: dictionary];
Run Code Online (Sandbox Code Playgroud)

我将哪些键值对添加到字典中以获取NSLineBreakByTruncatingTailNSTextAlignmentRight

nsstring ios ios7

46
推荐指数
2
解决办法
3万
查看次数

如果要包装的文本中有一个长字符串,sizeWithFont不会为UITextView提供正确的高度

有没有办法使用以下方法获得正确的NSString大小:

- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode
Run Code Online (Sandbox Code Playgroud)

不会被2或300个字符串抛出.目前,如果我尝试在这些长字符串上使用此方法,则会错误地计算它们,并且最终会在UITextView的底部显示大量空格.

我尝试过使用UILineBreakModeWordWrap和UILineBreakModeCharacterWrap.

调整大小正在进行中

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat     result = 44.0f;
    NSString*   text = nil;
    CGFloat     width = 0;
    CGFloat     tableViewWidth;
    CGRect      bounds = [UIScreen mainScreen].bounds;

    tableViewWidth = bounds.size.width;


    width = tableViewWidth - 150;       

    text = stringWithLongWords;

    if (text) {
        CGSize      textSize = { width, 20000.0f };     
        CGSize      size = [text sizeWithFont:[UIFont systemFontOfSize:10.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];

        size.height += 50.0f;               
        result = MAX(size.height, 44.0f+30.0f); 
    }

    return result;
}
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nsstring uitextfield

15
推荐指数
3
解决办法
1万
查看次数

在iOS 7中获取NSString高度

我使用下面的代码从字符串长度计算标签的高度.我使用xcode 5.0,它在iOS 6模拟器中工作正常,但它在iOS 7中运行不佳.

NSString* str = [[array objectAtIndex:i]valueForKey:@"comment"];

CGSize size = [className sizeWithFont:[UIFont systemFontOfSize:15]   
 constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

Height_1 = size.height;
Run Code Online (Sandbox Code Playgroud)

如果iOS 7有任何解决方案,那么请帮忙.提前致谢

iphone nsstring ipad cgsize

5
推荐指数
1
解决办法
2万
查看次数

sizeWithFont:ConstrainedToSize:在iOS 7中不推荐使用lineBreakMode:方法

sizeWithFont:ConstrainedToSize:lineBreakMode:方法在iOS 7中已弃用,我有点不确定如何处理这个问题.经过对互联网的一些研究后,我发现有一种新方法可以解决这个问题,即:

[txt drawWithRect: options: attributes: context:]
Run Code Online (Sandbox Code Playgroud)

这是我目前正在尝试运行的方法:

+ (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];

}
Run Code Online (Sandbox Code Playgroud)

但我很难将其转换为新方法.主要是使用lineBreakMode:在新方法中无处可去.有任何想法吗?

objective-c ios

2
推荐指数
1
解决办法
5932
查看次数

如何修复'sizeWithFont:constrainedToSize:lineBreakMode:'不推荐使用:警告

我的代码是

CGSize textSize =  [text sizeWithFont:font constrainedToSize:CGSizeMake(self.bounds.size.width - 106, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
Run Code Online (Sandbox Code Playgroud)

任何人都知道如何解决此警告?

objective-c ios deprecation-warning

0
推荐指数
1
解决办法
3296
查看次数