Ios 将Object-C转换为SWIFT textKit
在我的应用程序中,以下代码用于在粗体和斜体之间更改textview中文本的状态。我一直在尝试用Swift重新编写它,但我不断收到编译错误。下面是在线教程中提供给我的objective-C版本。接下来是我的新swift版本,直到方法崩溃。Ios 将Object-C转换为SWIFT textKit,ios,objective-c,swift,uifontdescriptor,Ios,Objective C,Swift,Uifontdescriptor,在我的应用程序中,以下代码用于在粗体和斜体之间更改textview中文本的状态。我一直在尝试用Swift重新编写它,但我不断收到编译错误。下面是在线教程中提供给我的objective-C版本。接下来是我的新swift版本,直到方法崩溃。 我将感谢任何可能帮助我理解编译器为何如此抱怨的帮助 -(void)addOrRemoveFontTraitWithName:(NSString *)traitName andValue:(uint32_t)traitValue{ NSRange selected
我将感谢任何可能帮助我理解编译器为何如此抱怨的帮助
-(void)addOrRemoveFontTraitWithName:(NSString *)traitName andValue:(uint32_t)traitValue{
NSRange selectedRange = [_field2 selectedRange];
NSDictionary *currentAttributesDict = [_field2.textStorage attributesAtIndex:selectedRange.location
effectiveRange:nil];
UIFont *currentFont = [currentAttributesDict objectForKey:NSFontAttributeName];
UIColor *currentColor = [currentAttributesDict objectForKey:NSForegroundColorAttributeName];
UIColor *currentBGColor = [currentAttributesDict objectForKey:NSBackgroundColorAttributeName];
UIFont *currentUnderlinedText = [currentAttributesDict objectForKey:NSUnderlineStyleAttributeName];
NSMutableParagraphStyle *currentparagraphStyle = [currentAttributesDict objectForKey:NSParagraphStyleAttributeName]; //receive and set new font name
UIFontDescriptor *fontDescriptor = [currentFont fontDescriptor];
NSString *fontNameAttribute = [[fontDescriptor fontAttributes] objectForKey:UIFontDescriptorNameAttribute];
UIFontDescriptor *changedFontDescriptor;
if ([fontNameAttribute rangeOfString:traitName].location == NSNotFound) {
uint32_t existingTraitsWithNewTrait = [fontDescriptor symbolicTraits] | traitValue;
changedFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:existingTraitsWithNewTrait];
}
else{
uint32_t existingTraitsWithoutTrait = [fontDescriptor symbolicTraits] & ~traitValue;
changedFontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:existingTraitsWithoutTrait];
}
UIFont *updatedFont = [UIFont fontWithDescriptor:changedFontDescriptor size:0.0];
//create new NSDict. called dict.
NSMutableDictionary *dict;
//call below function that returns an NSMutableDict. and places value to dict.
dict = [self checkSlectedAttributes:updatedFont SelFontColor:currentColor SelFontBGColor:currentBGColor SelunderLineState:currentUnderlinedText paragraphStyle:currentparagraphStyle];
//NSLog(@“dict为:%@”,dict)
}
SWIFT代码:
func addor removefonttraitwithname(traitName:String!、value-traitValue:UInt32){
}根据,UIFontDescriptorSymbolicTraits在Objective-C中定义为类型uint32\u t
,但在Swift中没有定义
尝试将代码更改为以下内容:
if fontNameAttribute.rangeOfString(traitName).location == NSNotFound {
let existingTraitsWithNewTrait : UInt32 = fontDescriptor.symbolicTraits.rawValue | traitValue
changedFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(rawValue: existingTraitsWithNewTrait))
} else {
let existingTraitsWithNewTrait : UInt32 = fontDescriptor.symbolicTraits.rawValue & traitValue
changedFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(rawValue: existingTraitsWithNewTrait))
}
如果您仍然需要访问UIFontDescriptorSymbolicTraits
的UInt32
值,您可以请求原始值:
var existingTraitsWithNewTraitUInt32:UInt32 = existingTraitsWithNewTrait.rawValue
你到底在为哪一部分而挣扎?if陈述中的所有内容。当我尝试使用swift编写此if语句时,我收到了关于|和&simbles的投诉,并且uint32到FontDescriptor的转换错误。我建议发布您当前的swift代码,以便我们可以看到您做错了什么。我尝试编写以下语句时遇到了问题:varexistingTraitsWithNewTrait=fontDescriptor.symbolicTraits | traitValue,我得到的错误是:无法使用类型为“(UIFontDescriptorSymbolicTraits,UInt32)”的参数列表调用“|”“我希望这有助于我的解释。你的第一个例子与我已经尝试过的差不多,但它的价值看起来很有希望。当我使用.rawValue时,if语句中的第一块代码起作用,但第二行抱怨UInt32不能转换为UIFontDescriptorSymbolicTraits。@EdwardG,第一块代码与您发布的代码不同。你真的试过吗?因为它对我有用。。。您不需要在当前代码中将任何内容转换为UInt32。是的,我收到了相同的错误。作为一名业余程序员,我正在尝试将代码从objective-C转换为Swift,因为这似乎是最直接的方法,但在Swift中,也许有一种不同或更简单的方法可以在粗体和斜体之间切换。有一件事我不明白,就是位运算符的用法,我不明白它们的用法,“|”(或)和“&”(和)符号是用来做什么的。@EdwardG我没有发现该代码有错误,所以您的代码一定在某种程度上有所不同。Swift编译器错误:无法使用类型为”(UIFontDescriptorSymbolicTraits,UInt32)的参数列表调用“|”
if fontNameAttribute.rangeOfString(traitName).location == NSNotFound {
let existingTraitsWithNewTrait : UInt32 = fontDescriptor.symbolicTraits.rawValue | traitValue
changedFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(rawValue: existingTraitsWithNewTrait))
} else {
let existingTraitsWithNewTrait : UInt32 = fontDescriptor.symbolicTraits.rawValue & traitValue
changedFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(rawValue: existingTraitsWithNewTrait))
}
var existingTraitsWithNewTraitUInt32:UInt32 = existingTraitsWithNewTrait.rawValue