Skip to content

Commit

Permalink
[attributedlabel] Add support for inline images.
Browse files Browse the repository at this point in the history
  • Loading branch information
jverkoey committed Aug 17, 2012
1 parent 00713ca commit 043ffcc
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//

#import "BasicInstantiationAttributedLabelViewController.h"

#import "NimbusAttributedLabel.h"

//
Expand Down
6 changes: 6 additions & 0 deletions examples/catalog/Catalog/CatalogViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#import "CustomTextAttributedLabelViewController.h"
#import "LinksAttributedLabelViewController.h"
#import "DataTypesAttributedLabelViewController.h"
#import "ImagesAttributedLabelViewController.h"
#import "PerformanceAttributedLabelViewController.h"
#import "LongTapAttributedLabelViewController.h"
#import "InterfaceBuilderAttributedLabelViewController.h"
Expand Down Expand Up @@ -163,6 +164,11 @@ - (id)initWithStyle:(UITableViewStyle)style {
toObject:
[NISubtitleCellObject objectWithTitle:@"Data Types"
subtitle:@"Detecting different data types"]],
[_actions attachNavigationAction:
NIPushControllerAction([ImagesAttributedLabelViewController class])
toObject:
[NISubtitleCellObject objectWithTitle:@"Images"
subtitle:@"Adding inline images"]],
[_actions attachNavigationAction:
NIPushControllerAction([PerformanceAttributedLabelViewController class])
toObject:
Expand Down
20 changes: 20 additions & 0 deletions examples/catalog/Catalog/ImagesAttributedLabelViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Copyright 2011-2012 Jeff Verkoeyen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import <UIKit/UIKit.h>

@interface ImagesAttributedLabelViewController : UIViewController
@end
98 changes: 98 additions & 0 deletions examples/catalog/Catalog/ImagesAttributedLabelViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//
// Copyright 2011-2012 Jeff Verkoeyen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "ImagesAttributedLabelViewController.h"

#import "NimbusAttributedLabel.h"

//
// What's going on in this file:
//
// This controller shows how to add inline images to NIAttributedLabel.
//
// You will find the following Nimbus features used:
//
// [attributedlabel]
// NIAttributedLabel
//
// This controller requires the following frameworks:
//
// Foundation.framework
// UIKit.framework
// CoreText.framework
// QuartzCore.framework
//

@interface ImagesAttributedLabelViewController ()
@end

@implementation ImagesAttributedLabelViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = @"Images";
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];

NIAttributedLabel* label = [[NIAttributedLabel alloc] initWithFrame:CGRectZero];

// When we assign the text we do not include any markup for the images.
label.text = @"This is Nimbus:He's a red panda.\nThis is a star:";

label.autoresizingMask = UIViewAutoresizingFlexibleDimensions;
label.lineBreakMode = UILineBreakModeWordWrap;
label.font = [UIFont systemFontOfSize:20];
label.textColor = [UIColor blackColor];
label.frame = CGRectInset(self.view.bounds, 20, 20);

// We want to insert an image directly after the string "Nimbus:" in the above string, so find its
// range and then use NSMaxRange to get the last index.
NSRange range = [label.text rangeOfString:@"Nimbus:"];

// When we insert an image we have the option to provide margins and vertical text alignment.
//
// Experiment:
// Try changing the values of the margins and seeing the effects it has.
//
[label insertImage:[UIImage imageNamed:@"Icon"]
atIndex:NSMaxRange(range)
margins:UIEdgeInsetsMake(5, 5, 5, 5)];

// We're going to add an image to the end of the string, so we pass the length of the string.
// Why not length - 1? Because the image gets inserted at the given index, so if we passed
// length - 1 the image would be immediately before the last character.
//
// Experiment:
// Try changing the index to other arbitrary values and seeing how it affects the way the text is
// drawn.
//
[label insertImage:[UIImage imageNamed:@"star"]
atIndex:label.text.length
margins:UIEdgeInsetsMake(5, 5, 5, 5)];

[self.view addSubview:label];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NIIsSupportedOrientation(interfaceOrientation);
}

@end
6 changes: 6 additions & 0 deletions examples/catalog/NimbusCatalog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
661F293315929E2300D11FC3 /* ApplicationBadges.xib in Resources */ = {isa = PBXBuildFile; fileRef = 661F293115929E2300D11FC3 /* ApplicationBadges.xib */; };
6633C7EF158F08A40054D240 /* InterfaceBuilderAttributedLabelViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6633C7EE158F08A40054D240 /* InterfaceBuilderAttributedLabelViewController.m */; };
6633C7F2158F08EA0054D240 /* AttributedLabelMashup.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6633C7F1158F08EA0054D240 /* AttributedLabelMashup.xib */; };
6633D21F15DDBD4100A4B07E /* ImagesAttributedLabelViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6633D21E15DDBD4100A4B07E /* ImagesAttributedLabelViewController.m */; };
66559AFA15C8BE6800ED9047 /* ActionsTableModelViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 66559AF915C8BE6800ED9047 /* ActionsTableModelViewController.m */; };
66559AFE15CC3F9B00ED9047 /* ModalRadioGroupTableModelViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 66559AFD15CC3F9B00ED9047 /* ModalRadioGroupTableModelViewController.m */; };
6677FEBD15AB8A0300517ABC /* BasicInstantiationTableModelViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6677FEBC15AB8A0300517ABC /* BasicInstantiationTableModelViewController.m */; };
Expand Down Expand Up @@ -145,6 +146,8 @@
6633C7ED158F08A40054D240 /* InterfaceBuilderAttributedLabelViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = InterfaceBuilderAttributedLabelViewController.h; sourceTree = "<group>"; };
6633C7EE158F08A40054D240 /* InterfaceBuilderAttributedLabelViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = InterfaceBuilderAttributedLabelViewController.m; sourceTree = "<group>"; };
6633C7F1158F08EA0054D240 /* AttributedLabelMashup.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = AttributedLabelMashup.xib; sourceTree = "<group>"; };
6633D21D15DDBD4100A4B07E /* ImagesAttributedLabelViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImagesAttributedLabelViewController.h; sourceTree = "<group>"; };
6633D21E15DDBD4100A4B07E /* ImagesAttributedLabelViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ImagesAttributedLabelViewController.m; sourceTree = "<group>"; };
66559AF815C8BE6800ED9047 /* ActionsTableModelViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ActionsTableModelViewController.h; sourceTree = "<group>"; };
66559AF915C8BE6800ED9047 /* ActionsTableModelViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ActionsTableModelViewController.m; sourceTree = "<group>"; };
66559AFC15CC3F9B00ED9047 /* ModalRadioGroupTableModelViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ModalRadioGroupTableModelViewController.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -664,6 +667,8 @@
669A15CC158D318B00A0990A /* LinksAttributedLabelViewController.m */,
669A15D1158E6C7C00A0990A /* DataTypesAttributedLabelViewController.h */,
669A15D2158E6C7C00A0990A /* DataTypesAttributedLabelViewController.m */,
6633D21D15DDBD4100A4B07E /* ImagesAttributedLabelViewController.h */,
6633D21E15DDBD4100A4B07E /* ImagesAttributedLabelViewController.m */,
669A15D4158E81D200A0990A /* PerformanceAttributedLabelViewController.h */,
669A15D5158E81D200A0990A /* PerformanceAttributedLabelViewController.m */,
66B6692D159BD22500FE4AE8 /* LongTapAttributedLabelViewController.h */,
Expand Down Expand Up @@ -971,6 +976,7 @@
66559AFE15CC3F9B00ED9047 /* ModalRadioGroupTableModelViewController.m in Sources */,
669819DF159B953A00C2D3EF /* NISnapshotRotation.m in Sources */,
6613335E15D3A66B00369333 /* SnapshotRotationTableViewController.m in Sources */,
6633D21F15DDBD4100A4B07E /* ImagesAttributedLabelViewController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
42 changes: 42 additions & 0 deletions src/attributedlabel/src/NIAttributedLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ typedef enum {
- (void)setStrokeColor:(UIColor *)color range:(NSRange)range;
- (void)setTextKern:(CGFloat)kern range:(NSRange)range;

- (void)insertImage:(UIImage *)image atIndex:(NSInteger)index;
- (void)insertImage:(UIImage *)image atIndex:(NSInteger)index margins:(UIEdgeInsets)margins;
- (void)insertImage:(UIImage *)image atIndex:(NSInteger)index margins:(UIEdgeInsets)margins verticalTextAlignment:(NIVerticalTextAlignment)verticalTextAlignment;

@property (nonatomic, assign) IBOutlet id<NIAttributedLabelDelegate> delegate;
@end

Expand Down Expand Up @@ -378,6 +382,44 @@ typedef enum {
* @fn NIAttributedLabel::setTextKern:range:
*/

/** @name Adding Inline Images */

/**
* Inserts the given image inline at the given index in the receiver's text.
*
* The image will have no margins.
* The image's vertical text alignment will be NIVerticalTextAlignmentBottom.
*
* @param image The image to add to the receiver.
* @param index The index into the receiver's text at which to insert the image.
* @fn NIAttributedLabel::insertImage:atIndex:
*/

/**
* Inserts the given image inline at the given index in the receiver's text.
*
* The image's vertical text alignment will be NIVerticalTextAlignmentBottom.
*
* @param image The image to add to the receiver.
* @param index The index into the receiver's text at which to insert the image.
* @param margins The space around the image on all sides in points.
* @fn NIAttributedLabel::insertImage:atIndex:margins:
*/

/**
* Inserts the given image inline at the given index in the receiver's text.
*
* @attention
* Images do not currently support NIVerticalTextAlignmentTop and the receiver will fire
* multiple debug assertions if you attempt to use it.
*
* @param image The image to add to the receiver.
* @param index The index into the receiver's text at which to insert the image.
* @param margins The space around the image on all sides in points.
* @param verticalTextAlignment The position of the text relative to the image.
* @fn NIAttributedLabel::insertImage:atIndex:margins:verticalTextAlignment:
*/

/** @name Accessing the Delegate */

/**
Expand Down
Loading

0 comments on commit 043ffcc

Please sign in to comment.