我有以下用Swift 3编写的简单代码:
let str = "Hello, playground"
let index = str.index(of: ",")!
let newStr = str.substring(to: index)
Run Code Online (Sandbox Code Playgroud)
从Xcode 9 beta 5,我得到以下警告:
'
substring(to:)'已被弃用:请使用String带有'partial range from'运算符的切片下标.
如何在Swift 4中使用具有部分范围的切片下标?
目前,我一直在使用XCode 8并在线
let range = key.startIndex...key.startIndex.advancedBy(0)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
错误:'advancedBy'不可用:要将索引前进n步,请在生成索引的CharacterView实例上调用'index(_:offsetBy :)'.
我怎样才能解决这个问题?
错误的屏幕截图如下:
我正在使用以下代码在iOS 8中生成NSAttributedStringfor UILabel.
// a long long Chinese title
NSString *title = @"?????????????????????";
// setup icon attachment
NSTextAttachment *iconAttachment = [[NSTextAttachment alloc] init];
iconAttachment.image = [UIImage imageNamed:imageName];
iconAttachment.bounds = bounds;
NSAttributedString *ycardImageString = [NSAttributedString attributedStringWithAttachment:iconAttachment];
// setup attributed text
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:title];
if (shouldShowYcard) {
[attributedText insertAttributedString:ycardImageString atIndex:0];
[attributedText insertAttributedString:[[NSAttributedString alloc] initWithString:@" "] atIndex:1];
[attributedText addAttribute:NSBaselineOffsetAttributeName value:@(offset) range:NSMakeRange(0, 1)];
}
NSRange titleRange = NSMakeRange(shouldShowYcard ? 2 : 0, title.length);
[attributedText addAttribute:NSFontAttributeName value:font range:titleRange];
[attributedText …Run Code Online (Sandbox Code Playgroud) 我正在使用目标c在ios应用程序上工作,我与uilabel有一个问题,我可以使用一些帮助.基本上我有一个标签,可以改变大小以适应它将显示的文本,但它有一个可能的最大高度.标签本身始终具有固定的宽度.我已经打开了UILineBreakModeWordWrap和UILineBreakModeTailTruncation来使文本适合并截断,但这会导致文本在它只剩下1个单词时过早地截断尾部.而当它仍有空间时,它将它移动到下一行,它只是截断它.
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, fixedWidth, 0);
self.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
self.numberOfLines = 0;
[self sizeToFit];
Run Code Online (Sandbox Code Playgroud)
无论如何找到uilabel实际截断文本的时间,然后我可以检查标签高度,如果还有空间则添加它?当有空间时,我总是尝试在高度上添加一条额外的线,这样可以避免早期截断,但是我会在整个标签上留下不一致的尺寸.任何想法都会非常感谢
我的iPhone项目中有一个UILabel,它有一个固定的宽度和高度,但它的内容可能会根据用户的不同而有所不同.有时文本对UILabel来说很大,那就是当字符串:'...'被添加到行尾时.我想知道我是否可以将此字符串更改为其他内容,例如:'(more)'.
谢谢!