2015-05-12 7 views
5

Possibile duplicate ma la risposta accettata è una semplice soluzione a una funzione.Ridimensionare un'immagine utilizzata in NSAattributedString

Sto cercando di rendere un articolo di notizie dettagli vista controller per visualizzare l'immagine dell'articolo in alto poi la data poi una lunga descrizione.

Ho imparato che devo usare NSAttributedString e solo uno UITextView per farlo. Ora tutto funziona bene, ma ho solo una questione ora: la larghezza dell'immagine non si addice la larghezza dello schermo (o la larghezza TextView), si prega di vedere questa immagine qui sotto:

App Screenshot

Ho provato di tutto, non funziona niente.

Come posso rendere l'immagine adatta allo schermo?

Grazie in anticipo.

A CURA DI NUOVO Questo è il codice che è coinvolto:

#import "SingleNewsViewController.h" 

@interface SingleNewsViewController() 

@end 

@implementation SingleNewsViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSAttributedString *image = [self imageString]; 
    NSAttributedString *date = [self dateString]; 
    NSAttributedString *body = [self bodyString]; 

    NSMutableAttributedString *wholeStory = [NSMutableAttributedString new]; 

    // TODO: can you be sure image, date and body are all non-nil? 
    NSArray *allComponents = @[image, date, body]; 
    for(NSAttributedString *component in allComponents) 
    { 
     [wholeStory appendAttributedString:component]; 
     if(component != [allComponents lastObject]) 
      [[wholeStory mutableString] appendString:@"\n\n"]; 
    } 

    self.tv.attributedText = wholeStory; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (UIImage *)image 
{ 
    return [UIImage imageNamed:@"latest_news_img.jpg"]; 
} 

- (NSString *)dateText 
{ 
    return @"Hi There, this is date!"; 
} 

- (NSString *)bodyText 
{ 
    return @"Hi There, this is body text!Hi There, ....."; 
} 

- (NSAttributedString *)imageString 
{ 
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; 
    textAttachment.image = [self image]; 
    return [NSAttributedString attributedStringWithAttachment:textAttachment]; 
} 

- (NSAttributedString *)dateString 
{ 
    return [[NSAttributedString alloc] 
      initWithString:[self dateText] 
      /*attributes: 
      @{ 
      NSFontAttributeName: [UIFont preferredFontForTextStyle: UIFontTextStyleSubheadline], 
      ... etc ... 
      }*/]; 
} 

- (NSAttributedString *)bodyString 
{ 
    return [[NSAttributedString alloc] 
      initWithString:[self bodyText] 
      /*attributes: 
      @{ 
      NSFontAttributeName: [UIFont preferredFontForTextStyle: UIFontTextStyleBody], 
      ... etc ... 
      }*/]; 
} 



@end 
+0

Imposta la proprietà '' 'UIImageView''' su' 'Scale to Fit'''. –

+0

ho fatto tutto questo vista a livello di codice, utilizzando NSAttributedString, non lo storyboard – ANA

+0

ho bisogno di una soluzione programmatica – ANA

risposta

0

Oh giusto, in modo da NSTextAttachment non dispongono di vista di immagine ... ah ... Proprio così, allora un'altra idea viene in mente . Che ne dite di reszing i dati UIImage e poi passando quell'immagine al NSTextAttachment, come in questo post:

https://stackoverflow.com/a/2658801/4657588

Fare uso di questo metodo per ridimensionare l'immagine di conseguenza:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize { 
    //UIGraphicsBeginImageContext(newSize); 
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution). 
    // Pass 1.0 to force exact pixel size. 
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); 
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext(); 
    return newImage; 
} 
+0

'Immagine proprietàView non trovata su oggetto di tipo NSTextAttachment' – ANA

+0

perché non c'è la vista immagine – ANA

+0

NSTextAttachment non ha una proprietà' imageView' – Lance