-2

私のアプリでは、次のコードを使用して、テキストビュー内のテキストの状態を太字と斜体の間で変更しました。Swift で書き直そうとしていますが、コンパイル エラーが発生し続けます。以下は、オンライン チュートリアルで提供された Objective-C のバージョンです。それに続いて、メソッドがクラッシュするまでの私の新しい迅速なバージョンがあります。
コンパイラがなぜそれほど不平を言っているのかを理解するのに役立つかもしれない助けを感謝します。

-(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(@"the dict is: %@", dict);

[_field2.textStorage beginEditing];
[_field2.textStorage setAttributes:dict range:selectedRange];
[_field2.textStorage endEditing];

}

SWIFT CODE: func addOrRemoveFontTraitWithName(traitName: String!, andValue traitValue: UInt32) {

    let selectedRange : NSRange = self.textView.selectedRange

    var currentAttributesDict : NSDictionary = textView.textStorage.attributesAtIndex(selectedRange.location, effectiveRange: nil)

    var currentFont : UIFont = currentAttributesDict .objectForKey(NSFontAttributeName) as UIFont

    var currentColor : UIColor = currentAttributesDict.objectForKey(NSForegroundColorAttributeName) as UIColor
    var currentBGColor : UIColor = currentAttributesDict.objectForKey(NSBackgroundColorAttributeName) as UIColor
    var currentUnderLinedText : UIFont = currentAttributesDict.objectForKey(NSUnderlineStyleAttributeName) as UIFont
    var currentparagraphStyle : NSMutableParagraphStyle = currentAttributesDict.objectForKey(NSParagraphStyleAttributeName) as NSMutableParagraphStyle

    var fontDescriptor : UIFontDescriptor = currentFont.fontDescriptor()
    var fontNameAttribute : NSString = fontDescriptor.objectForKey(UIFontDescriptorNameAttribute) as NSString
    var changedFontDescriptor : UIFontDescriptor

    if (fontNameAttribute.rangeOfString(traitName).location == NSNotFound){
       var existingTraitsWithNewTrait : UInt32 = fontDescriptor.symbolicTraits | traitValue
        changedFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(existingTraitsWithNewTrait)
    }else{

        var existingTraitsWithNewTrait : UInt32 = fontDescriptor.symbolicTraits & ~traitValue
        changedFontDescriptor = fontDescriptor.fontDescriptorWithSymbolicTraits(existingTraitsWithNewTrait)
    }

}

4

1 に答える 1

5

の "Symbolic Font Traits" のドキュメントUIFontDescriptorよると、UIFontDescriptorSymbolicTraitsタイプuint32_tは Objective-C で定義されていますが、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))
}

UInt32それでも値にアクセスする必要があるUIFontDescriptorSymbolicTraits場合は、生の値をリクエストできます。

var existingTraitsWithNewTraitUInt32:UInt32 = existingTraitsWithNewTrait.rawValue
于 2015-03-10T05:08:58.367 に答える