7
votes

UisegmentedControl avec image et titre

J'utilise un UisegmentedControl code> à l'intérieur d'un uitoolbar code> comme bouton. Je ne peux pas utiliser d'uibuttonbaritem normal code>, car je dois définir le TintColor code> et prendre en charge iOS 4.3. Donc, j'utilise le code suivant:

NSString* label = @"My Label";
UIImage* image = [self addText:label toImage:[UIImage imageNamed:@"icon"]];
[_segmentedControl insertSegmentWithImage:image atIndex:0 animated:NO];
[_segmentedControl setWidth:image.size.width + 10]; // add some margin on the right



- (UIImage *)addText:(NSString *)text toImage:(UIImage *)image {
    UIFont* font = [UIFont boldSystemFontOfSize: 12];
    CGSize expectedSize = [text sizeWithFont:font];

    [self retinaAwareUIGraphicsBeginImageContext:CGSizeMake(image.size.width + expectedSize.width + 10, image.size.height)];
    [image drawAtPoint: CGPointZero];
    [[UIColor whiteColor] set];
    [text drawAtPoint: CGPointMake(30, 2) withFont:font];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

- (void)retinaAwareUIGraphicsBeginImageContext:(CGSize) size {
    CGFloat scale = -1.0;
    if (scale<0.0) {
        UIScreen *screen = [UIScreen mainScreen];
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) {
            scale = [screen scale];
        }
        else {
            scale = 0.0;
        }
    }
    if (scale>0.0) {
        UIGraphicsBeginImageContextWithOptions(size, NO, scale);
    }
    else {
        UIGraphicsBeginImageContext(size);
    }
}


0 commentaires

7 Réponses :


-4
votes

Essayez ceci:

UISegmentedControl* searchButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"", nil]];
[searchButton insertSegmentWithImage:[UIImage imageNamed:@"up_button.png"] atIndex:0 animated:YES];
[searchButton insertSegmentWithImage:[UIImage imageNamed:@"down_button.png"] atIndex:1 animated:YES];
searchButton.segmentedControlStyle = UISegmentedControlStyleBar;
searchButton.tintColor = [UIColor colorWithRed:0.27 green:0.60 blue:0.20 alpha:1.00];
[searchButton setMomentary:YES];


1 commentaires

Je ne pense pas que c'est possible. La documentation indique ce qui suit: Un contrôle segmenté peut afficher un titre (objet Nstring) ou une image (objet UIIMAGE). Si vous souhaitez définir le titre: [SearchButton Instetsewithtitle: @ "Up" AttinDex: 0 animé: Non]; [SearchButton Insertswithtitle: @ "Down" AttinDex: 1 animé: Non];



23
votes

Vous pouvez le faire:

Ajouter une catégorie pour UIIMAGE: P>

in uiimage + uisegmentIndtext.h em> p> xxx pré> > dans uiimage + uisegmenticonandtext.m em> p> xxx pré>

dans votre code de code segment: p>

UIImage *imageWithText = [UIImage imageFromImage:[UIImage imageNamed:@"images/jobaids_tab_icon"] string:@"Hello", nil) color:[UIColor whiteColor]];

[_segmentedController setImage:imageWithText forSegmentAtIndex:0];


2 commentaires

Réponse parfaite :) merci


Fonctionne bien, mais les tailles de police sont éteintes. La taille de texte semble devenir plus petite car la taille de la chaîne augmente



1
votes

pour SWIFT.

segfont est la police du texte intégré.

ImageAlInment est l'alignement de l'image apparaissant à gauche ou à droite du texte.

SET ImageAlIGNMENT à 0 pour l'alignement gauche (l'image apparaît à gauche du texte) et 1 pour un alignement droit (l'image apparaîtra à droite du texte) xxx


0 commentaires

0
votes

SWIFT 3 STRUT>

extension UIImage{


class func textEmbededImage(image: UIImage, string: String, color:UIColor, imageAlignment: Int = 0, segFont: UIFont? = nil) -> UIImage {
    let font = segFont ?? UIFont.systemFont(ofSize: 16.0)
    let expectedTextSize: CGSize = (string as NSString).size(attributes: [NSFontAttributeName: font])
    let width: CGFloat = expectedTextSize.width + image.size.width + 5.0
    let height: CGFloat = max(expectedTextSize.height, image.size.width)
    let size: CGSize = CGSize(width: width, height: height)
    UIGraphicsBeginImageContextWithOptions(size, false, 0)
    let context: CGContext = UIGraphicsGetCurrentContext()!
    context.setFillColor(color.cgColor)
    let fontTopPosition: CGFloat = (height - expectedTextSize.height) / 2.0
    let textOrigin: CGFloat = (imageAlignment == 0) ? image.size.width + 5 : 0
    let textPoint: CGPoint = CGPoint.init(x: textOrigin, y: fontTopPosition)        
    string.draw(at: textPoint, withAttributes: [NSFontAttributeName: font])
    let flipVertical: CGAffineTransform = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: size.height)
    context.concatenate(flipVertical)
    let alignment: CGFloat =  (imageAlignment == 0) ? 0.0 : expectedTextSize.width + 5.0
    context.draw(image.cgImage!, in: CGRect.init(x: alignment, y: ((height - image.size.height) / 2.0), width: image.size.width, height: image.size.height))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage!
}

}


0 commentaires

7
votes

SWIFT 4 xxx


1 commentaires

Merci d'avoir déplacé la réponse précédente à Swift!



4
votes

Swift 5

Pour ceux d'entre nous qui aiment utiliser une API moderne partout où il est possible: xxx


0 commentaires

0
votes

Swift 5

Je devais ajouter une image sur le coin à droite, donc j'ai ajouté imageOffset. xxx

  • Le texte qui se chevauche de l'image n'est pas implémenté
  • Lorsque l'imageBeforetext est true, Minus x Décalage signifie plus d'espace entre l'image et le texte.
  • Utilisé avec RenfluentationMode (andalwayiginal) pour obtenir différentes couleurs pour le texte et l'image

0 commentaires