Material-UI next - 在ListItemText中设置样式文本

时间:2017-05-15 09:28:16

标签: reactjs material-ui

我试图将样式应用于ListItemText(Material-UI @next)中的文本:

<Typograhy>

但内部呈现的NSArray *activityItems = [NSArray arrayWithObjects:[NSString stringWithFormat:@"Sample text", nil]; UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil]; activityViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; [activityViewController setCompletionHandler:^(NSString *act, BOOL done) { //Code here for completion handler }]; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { [self presentViewController:activityViewController animated:YES completion:nil]; } //if iPad else { // Change Rect to position Popover UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityViewController]; [popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; } 元素根本没有样式(&#34; MyText&#34;不是红色)。

查看生成的代码,似乎是Typography的默认CSS规则&gt;副标题覆盖了我的CSS。

感谢您的帮助

编辑:在问题的第一个版本中,有一个错误(&#34; className&#34;而不是&#34;样式&#34;在ListItemText上支持,对不起)。

10 个答案:

答案 0 :(得分:19)

我相信现在实现这一目标的唯一方法就是使用&#39; disableTypography&#39; ListItemText元素的prop。

 <ListItemText
        disableTypography
        primary={<Typography type="body2" style={{ color: '#FFFFFF' }}>MyTitle</Typography>}
      />

这使您可以使用自己想要的样式嵌入自己的文本元素。

答案 1 :(得分:8)

事实证明,有一种更好的方法可以做到这一点:

const styles = {
  selected: {
    color: 'green',
    background: 'red',
  },
}

const DashboardNagivationItems = props => (
  <ListItemText
    classes={{ text: props.classes.selected }}
    primary="Scheduled Calls"
  />
)

export default withStyles(styles)(DashboardNagivationItems)

您可以在此处详细了解如何完成此操作:https://material-ui-next.com/customization/overrides/#overriding-with-classes

答案 2 :(得分:7)

            <ListItem >
                    <Avatar style={{ backgroundColor: "#ff6f00" }}>
                      <LabIcon />
                    </Avatar>
                    <ListItemText 
                     primary={<Typography variant="h6" style={{ color: '#ff6f00' }}>Lab</Typography>}
                     secondary="Experiments" />
                  </ListItem>

enter image description here

答案 3 :(得分:4)

this是不错的选择,您可以在不禁用排版的情况下实现

<ListItemText 
   classes={{ primary: this.props.classes.whiteColor }}
   primary="MyTitle"
/>

答案 4 :(得分:3)

材料v1.0

我会在@SundaramRavi上添加一些内容:

  • 他使用的风格元素的方式与Material v1.0不同(阅读v0.16+v1.0之间非常重要的区别。
  • 可以构建文件的方式,以便更好地分离关注点。

Whatever.styles.js

const styles = theme => ({
  white: {
    color: theme.palette.common.white
  }
});

exports.styles = styles;

Whatever.js

const React = require('react');
const PropTypes = require('prop-types');
const {styles} = require('./Whatever.styles');

class Whatever extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    const {classes} = this.props;
    return (
      <div>
        <ListItemText
          disableTypography
          primary={
            <Typography type="body2" style={{body2: classes.white}}>
              MyTitle
            </Typography>
          }
        />
      </div>
    );
  }
}

Whatever.propTypes = {
  classes: PropTypes.object.isRequired,
  theme: PropTypes.object.isRequired
};

exports.Whatever = withStyles(styles, {withTheme: true})(Whatever);

答案 5 :(得分:1)

方法1

const textColor = {
    color: "red"
};

<ListItemText primaryTypographyProps={{ style: textColor }} primary="BlodyLogic" />
对于次要文字
const secondaryColor = {
   color: 'green'
}

<ListItemText secondaryTypographyProps={{ style: secondaryColor }} 
     secondary="If you say that you" />

方法2

<ListItemText
          primary={
            <Typography variant="caption" display="block" gutterBottom>
              caption text
            </Typography>
          }
 />

定制设计:

const useStyles = makeStyles({
      text: {
        color: 'red',
        fontSize: 49
      },
    });
        
/.....// all make classes

<ListItemText
              primary={
                <Typography className={classes.text}>
                  caption text
                </Typography>
              }
     />

Offical Docs

答案 6 :(得分:1)

如果您使用的是 "@material-ui/core": "^4.11.4"(4.X.X 或更新版本) 那么很简单:

#1st 步骤:像这样定义您的样式:

const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    // Other Styling if you wish to use it
    root: {
      width: '100%',
      maxWidth: '36ch',
      backgroundColor: theme.palette.background.paper
    },
    // Other Styling if you wish to use it
    inline: {
      display: 'inline'
    },
    // Styling that you asked for
    textColor: {
        color: 'red'
    }
  }),
);

#2nd 步骤:定义一个常量使用特定样式像这样:

const AlignItemsList = (props: any) => {
    const classes = useStyles(); // Like this one
    ......
}

#3rd 步骤:在您的 ListItemText 组件中执行以下操作:

const AlignItemsList = (props: any) => {
    const classes = useStyles();
    ......
    <ListItemText
         primary="Your Text Goes Here"
         classes={{ primary: classes.textColor }} // Like this one
         ...
   />
};

#4th & 最后一步:只需正常导出您的组件,无需任何其他内容,如下所示:

export default AlignItemsList;

答案 7 :(得分:0)

如果您使用的是Material-ui 3.x,请按以下步骤操作:

import { withStyles } from '@material-ui/core/styles';

const styles = {
  listItemText: {
    color: 'white',
  },
}

class YourComponent extends Component {
...
render() {
    const { classes } = this.props; // this is magically provided by withStyles HOC.
    return (
          <ListItem button>
            <ListItemIcon>
              <DashboardIcon />
            </ListItemIcon>
            <ListItemText classes={{ primary: classes.listItemText }} primary="My Bookings" />
          </ListItem>
    );
...

}
export default withStyles(styles)(YourComponent);

primary属性中设置所有与文本相关的样式。遗憾的是它在文档中隐藏得如此之深。 https://material-ui.com/api/list-item/

答案 8 :(得分:0)

根据documentation<ListItemText />组件将显示道具primaryTypographyProps,我们可以使用它来完成您在问题中要尝试的工作:

const text = {
    color: "red"
};

<ListItem button>
    <ListItemText primaryTypographyProps={{ style: text }} primary="MyText" />
</ListItem>

希望有帮助!

答案 9 :(得分:0)

您可以使用& span

轻松设置文本样式
const useStyles = makeStyles(theme => ({
    listItem: {
        "& span": {
            color: "red"
        }

    }
}));
..
..
..

<ListItem button>
    <ListItemIcon>
        <SendIcon />
    </ListItemIcon>
    <ListItemText className={classes.listItem} primary="Sent mail"/>
</ListItem>